package Phonogram;

import java.io.*;

/**
 * @Author shenxinyuan
 * @Description //TODO 
 * @Date $ time$ 2021/9/15
 * @Param 
 * @return 
 **/
/** 音 阶              1   2	 3	 4	 5   6   7 	 1*
 * 低频率(单位:Hz)	262	294	330	347	392	440	494	524
 * 高频率(单位:Hz)	524	588	660	698	784	880	988	1048    +30...(再高的音阶酌情增加)
 */
public class PhonogramToFrequency {
    //                              （dou ruai   mi  fa   sou   la  xi的高频率，单位hz）
    private int frequencySrcLow[] = {262, 294, 330, 347, 392, 440, 494, 524};         //存放原低标准频率表
    private int frequencySrcHigh[] = {524, 588, 660, 698, 784, 880, 988, 1048};         //存放原高标准频率表

    //private int timeSrc[];                  //存放节拍的延迟时间表（单位是节拍个数）

    private int frequencyDest[] = new int[999];            //存放歌曲转换过来的频率表（hz）
    //private int timeDest[];                 //存放歌曲转换过来的延迟时间表（节拍数）

    private String fileName;                //原频率表和时间表存放的文件的位置

    private int i = 0;

    public PhonogramToFrequency(String fileName) {
        this.fileName = fileName;
    }


    /**
     * 2 以字符为单位读取文件，常用于读文本，数字等类型的文件
     */
    public void readFileByChars() {
        File file = new File(fileName);
        Reader reader = null;
        try {
            // 一次读一个字符
            reader = new InputStreamReader(new FileInputStream(file));
            int tempCharPre = 0;
            int tempChar;
            while ((tempChar = reader.read()) != -1) {
                if (((char) tempChar) != '\r') {
                    System.out.print((char) tempChar);
                    if (!Character.isSpace((char) tempChar)) {            //空格的assci码是：32
                        if ((char) tempChar == '+') {
                            i--;
                            frequencyDest[i] = frequencySrcHigh[tempCharPre-1];

                        } else if ((char) tempChar == '-') {
                            i--;
                            frequencyDest[i] = frequencySrcLow[tempCharPre-1];
                        } else {
                            frequencyDest[i] = frequencySrcHigh[tempChar - '0'-1];
                            tempCharPre = tempChar - '0';
                        }
                        i++;
                    }
                }
            }
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //把数据写入文件
    private void writeResult() throws IOException {
        File file = new File("resultFrequency.txt");  //存放数组数据的文件
        if (!file.exists()) {
            file.createNewFile();
        }
        try {
            FileWriter fw = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
            StringBuilder str = new StringBuilder("{");
            for (int x = 0; x < i; x++) {
                str.append(frequencyDest[x]);
                if (x < i - 1) {
                    str.append(",");
                }
            }
            str.append("}");
            bw.write(str.toString());
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    private void showFrequency() {
        int y=0;
        System.out.print("{");
        for (int x = 0; x < i; x++,y++) {
            System.out.print(frequencyDest[x]);
            if (x < i - 1) {
                System.out.print(",");
            }
            if(y==36){
                y=0;
                System.out.println();
            }
        }
        System.out.print("}");
    }

    public static void main(String[] args) throws IOException {
        String fileName = "I:\\微机播放歌曲\\歌唱不0.txt";
        PhonogramToFrequency pho = new PhonogramToFrequency(fileName);
        pho.readFileByChars();          //读取原音阶
        System.out.println();

        pho.showFrequency();            //展示转换后的频率表
        pho.writeResult();              //写入文件
    }
}
